﻿using System;
using System.Globalization;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using equinoxWeb.GestionConexion;
using equinoxWeb.Models;
using MigraDoc.DocumentObjectModel;
using MigraDoc.DocumentObjectModel.Shapes;
using MigraDoc.DocumentObjectModel.Tables;
using MigraDoc.Rendering;
using PdfSharp.Drawing;
using PdfSharp.Pdf;

namespace equinoxWeb.Controllers
{
    public class pdfController : EquinoxController
    {
        // GET: /pdf/WorkingSheet
        public ActionResult WorkingSheet(int? idCaso, int? idDocumento)
        {
            if (defaultAction != null) return defaultAction;

            Caso caso = CasoFunction.abrirCaso(idCaso.Value);
            caso.Costos = FacturacionFunction.listaDetalleFacturacion(idCaso.Value, idDocumento.Value);
            Facturacion fact = FacturacionFunction.abrirFacturacion(idCaso.Value, idDocumento.Value);
            Document document = new Document();
            document.Info.Title = "Working Sheet";
            document.Info.Author = "Equinox Consulting";
            string margins = "1.5cm";
            document.DefaultPageSetup.LeftMargin = margins;
            document.DefaultPageSetup.RightMargin = margins;
            document.DefaultPageSetup.PageFormat = MigraDoc.DocumentObjectModel.PageFormat.A4;
            definirEstilosWS(document);
            crearPdfWS(document, caso, fact);
            const bool unicode = false;
            const PdfFontEmbedding embedding = PdfFontEmbedding.Always;
            PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(unicode, embedding);
            pdfRenderer.Document = document;
            pdfRenderer.RenderDocument();
            MemoryStream stream = new MemoryStream();
            pdfRenderer.PdfDocument.Save(stream, false);
            return File(stream.ToArray(), "application/pdf");
        }

        private void definirEstilosWS(Document document)
        {
            //obtener el estilo "Normal" y editar desde ahi
            Style style = document.Styles["Normal"];
            //poner esta font para todo el dcumento
            style.Font.Name = "Times New Roman";
            style.Font.Size = 10;

            style = document.Styles["Heading1"];
            style.Font.Size = 14;
            style.Font.Bold = true;
            style.ParagraphFormat.PageBreakBefore = true;
            style.ParagraphFormat.SpaceBefore = 6;
            style.ParagraphFormat.SpaceAfter = 6;

            style = document.Styles["Heading2"];
            style.Font.Size = 12;
            style.Font.Bold = true;
            style.ParagraphFormat.PageBreakBefore = false;
            style.ParagraphFormat.SpaceBefore = 6;
            style.ParagraphFormat.SpaceAfter = 6;

            style = document.Styles["Heading3"];
            style.Font.Size = 10;
            style.Font.Bold = true;
            style.ParagraphFormat.PageBreakBefore = false;
            style.ParagraphFormat.SpaceBefore = 6;
            style.ParagraphFormat.SpaceAfter = 6;

            style = document.Styles["Heading4"];
            style.Font.Size = 10;
            style.Font.Bold = true;
            style.ParagraphFormat.PageBreakBefore = false;
            style.ParagraphFormat.SpaceBefore = 6;
            style.ParagraphFormat.SpaceAfter = 6;

            style = document.Styles.AddStyle("Table", "Normal");
            style.Font.Size = 10;
        }

        private void crearPdfWS(Document document, Caso caso, Facturacion fact)
        {
            NumberFormatInfo nfi = (NumberFormatInfo)
            CultureInfo.InvariantCulture.NumberFormat.Clone();
            nfi.NumberGroupSeparator = " ";
            Section section = document.AddSection();
            //Imagen y nombre de la empresa
            Paragraph nombre = document.LastSection.AddParagraph();
            nombre.Style = "Heading1";
            nombre.Format.Alignment = ParagraphAlignment.Center;
            nombre.Format.Font.Color = Colors.DodgerBlue;
            Image img = nombre.AddImage(Server.MapPath("~/Content/site/images/logo-small-text.jpg"));
            //nombre.AddText("EQUINOX CONSULTING S.A.C.");

            //Titulo del documento
            Paragraph titulo = document.LastSection.AddParagraph();
            titulo.Format.Alignment = ParagraphAlignment.Center;
            titulo.Style = "Heading2";
            titulo.AddFormattedText("WORKING SHEET",TextFormat.Bold);
            titulo.Format.Font.Name = "Times New Roman";
            titulo.Format.Borders.Width = 2.5;
            titulo.Format.Borders.Color = Colors.Black;
            titulo.Format.Borders.Distance = 3;

            //Reference and rate (table)
            Table tablaRef = new Table();
            tablaRef.Borders.Width = 0.75;
            Column column = tablaRef.AddColumn(Unit.FromCentimeter(3));
            column = tablaRef.AddColumn(Unit.FromCentimeter(2));
            column.Format.Alignment = ParagraphAlignment.Center;
            column = tablaRef.AddColumn(Unit.FromCentimeter(2));
            column.Format.Alignment = ParagraphAlignment.Center;
            column = tablaRef.AddColumn(Unit.FromCentimeter(2));
            column.Format.Alignment = ParagraphAlignment.Center;
            Row fila =tablaRef.AddRow();
            fila.Borders.Visible = false;
            string referencia = CasoFunction.obtenerReferencia(caso.IdCaso);
            string[] refs = referencia.Split("/".ToCharArray());

            fila.Cells[0].AddParagraph("Reference");
            fila.Cells[1].AddParagraph(refs[0]);
            fila.Cells[2].AddParagraph(refs[1]);
            fila.Cells[3].AddParagraph(refs[2]);
            tablaRef.SetEdge(1, 0, 3, 1, Edge.Box | Edge.Interior, BorderStyle.Single, 0.75);

            document.LastSection.Add(tablaRef);

            Paragraph rate = document.LastSection.AddParagraph();
            rate.Format.Alignment = ParagraphAlignment.Right;
            Table tablaRate = new Table();
            tablaRate.AddColumn(Unit.FromCentimeter(14.25));
            column = tablaRate.AddColumn(Unit.FromCentimeter(2));
            column.Format.Alignment = ParagraphAlignment.Left;
            tablaRate.AddColumn(Unit.FromCentimeter(2));
            Row f = tablaRate.AddRow();
            f.Cells[1].AddParagraph("Rate: ");
            f.Cells[2].AddParagraph(fact.Rate.ToString("N",nfi));
            tablaRate.Format.Alignment = ParagraphAlignment.Right;
            tablaRate.SetEdge(1, 0, 2, 1, Edge.Box | Edge.Interior, BorderStyle.Single, 0.75);
            document.LastSection.Add(tablaRate);
            

            document.LastSection.AddParagraph();

            //Tabla datos
            Table table = new Table();
            //table.Borders.Width = 0.75;

            column = table.AddColumn(Unit.FromCentimeter(2.25));
            column.Format.Alignment = ParagraphAlignment.Right;

            column = table.AddColumn(Unit.FromCentimeter(14.5));
            column.Format.Alignment = ParagraphAlignment.Left;

            Row row = table.AddRow();
            Cell cell = row.Cells[0];
            cell.AddParagraph("At Request of");
            cell = row.Cells[1];
            Paragraph und = cell.AddParagraph();
            und.AddFormattedText(caso.Cliente != null ? caso.Cliente.RazonSocial : "");
            

            row = table.AddRow();
            cell = row.Cells[0];
            cell.AddParagraph("On Behalf of");
            cell = row.Cells[1];
            und = cell.AddParagraph();
            und.AddFormattedText(fact.Facturara!= null && fact.Facturara != "" ? fact.Facturara : caso.Cliente.RazonSocial);
            row = table.AddRow();
            cell = row.Cells[0];
            cell.AddParagraph("Abstract");

            cell = row.Cells[1];
            und = cell.AddParagraph();
            und.AddFormattedText(caso.Content != null ? caso.Content : "",TextFormat.Underline);
            table.SetEdge(1, 0, 1, 3, Edge.Bottom | Edge.Interior, BorderStyle.Single, 0.75);
            document.LastSection.Add(table);

            document.LastSection.AddParagraph();

            //Tabla traveling hours
            table = new Table();
            table.Style = "Table";
            table.Borders.Width = 0.75;
            //Date
            column = table.AddColumn(Unit.FromCentimeter(2));
            column.Format.Alignment = ParagraphAlignment.Center;
            //from
            column = table.AddColumn(Unit.FromCentimeter(1.25));
            column.Format.Alignment = ParagraphAlignment.Center;
            //to
            column = table.AddColumn(Unit.FromCentimeter(1.25));
            column.Format.Alignment = ParagraphAlignment.Center;
            //hours
            column = table.AddColumn(Unit.FromCentimeter(1.75));
            column.Format.Alignment = ParagraphAlignment.Center;
            //description
            column = table.AddColumn(Unit.FromCentimeter(7.5));
            column.Format.Alignment = ParagraphAlignment.Left;
            //Surveyor
            column = table.AddColumn(Unit.FromCentimeter(2.5));
            column.Format.Alignment = ParagraphAlignment.Center;
            //expense
            column = table.AddColumn(Unit.FromCentimeter(2));
            column.Format.Alignment = ParagraphAlignment.Center;

            //header
            row = table.AddRow();
            row.Borders.Width = 1.25;
            Paragraph p = row.Cells[0].AddParagraph();
            p.AddFormattedText("TRAVELING HOURS",TextFormat.Bold);
            row.Cells[0].Format.Alignment = ParagraphAlignment.Center;
            row.Cells[0].MergeRight = 6;

            row = table.AddRow();
            row.Cells[0].AddParagraph("Date");
            row.Cells[1].AddParagraph("from");
            row.Cells[2].AddParagraph("to");
            row.Cells[3].AddParagraph("hours");
            row.Cells[4].AddParagraph("Description");
            row.Cells[4].Format.Alignment = ParagraphAlignment.Center;
            row.Cells[5].AddParagraph("Surveyor");
            row.Cells[6].AddParagraph("Expense");

            //listas de detalle
            List<DetalleFacturacion> tipo1 = new List<DetalleFacturacion>();
            List<DetalleFacturacion> tipo2 = new List<DetalleFacturacion>();
            List<DetalleFacturacion> tipo3 = new List<DetalleFacturacion>();

            foreach (DetalleFacturacion d in caso.Costos)
            {
                switch (d.TipoCosto)
                {
                    case 1:
                        tipo1.Add(d);
                        break;
                    case 2:
                        tipo2.Add(d);
                        break;
                    case 3:
                        tipo3.Add(d);
                        break;
                }
            }

            float totalCost = 0;
            float travelingHours = 0;
            foreach (DetalleFacturacion c in tipo1)
            {
                row = table.AddRow();
                row.VerticalAlignment = VerticalAlignment.Center;
                row.Format.Font.Size = 9;
                row.Cells[0].AddParagraph(c.Fecha.ToString());
                row.Cells[1].AddParagraph(c.HoraIni.ToString());
                row.Cells[2].AddParagraph(c.HoraFin.ToString());
                float hours = c.HorasSubtotales;
                row.Cells[3].AddParagraph(hours.ToString("N",nfi));
                row.Cells[4].AddParagraph(c.Descripcion);
                row.Cells[5].AddParagraph(c.Surveyor);
                row.Cells[6].AddParagraph("$"+c.Costo.ToString("N",nfi));
                totalCost += c.Costo;
                travelingHours += hours;
            }
            Row totalRow = table.AddRow();
            totalRow.VerticalAlignment = VerticalAlignment.Center;
            cell = totalRow.Cells[0];
            p = cell.AddParagraph();
            p.AddFormattedText("Total hours", TextFormat.Bold);
            totalRow.Cells[1].MergeRight=1;
            p= totalRow.Cells[3].AddParagraph();
            p.AddFormattedText(travelingHours.ToString("N",nfi),TextFormat.Bold);
            totalRow.Cells[4].MergeRight = 1;
            p= totalRow.Cells[6].AddParagraph();
            p.AddFormattedText("$"+totalCost.ToString("N",nfi),TextFormat.Bold);

            //waiting hours
            //header
            row = table.AddRow();
            row.Borders.Width = 1.25;
            p = row.Cells[0].AddParagraph();
            p.AddFormattedText("WAITING HOURS", TextFormat.Bold);
            row.Cells[0].Format.Alignment = ParagraphAlignment.Center;
            row.Cells[0].MergeRight = 6;

            totalCost = 0;
            float waitingHours = 0;
            foreach (DetalleFacturacion c in tipo3)
            {
                row = table.AddRow();
                row.VerticalAlignment = VerticalAlignment.Center;
                row.Cells[0].AddParagraph(c.Fecha.ToString());
                row.Cells[1].AddParagraph(c.HoraIni.ToString());
                row.Cells[2].AddParagraph(c.HoraFin.ToString());
                float hours = c.HorasSubtotales;
                row.Cells[3].AddParagraph(hours.ToString("N",nfi));
                row.Cells[4].AddParagraph(c.Descripcion);
                row.Cells[5].AddParagraph(c.Surveyor);
                row.Cells[6].AddParagraph("$" + c.Costo.ToString("N",nfi));
                totalCost += c.Costo;
                waitingHours += hours;
            }
            totalRow = table.AddRow();
            totalRow.VerticalAlignment = VerticalAlignment.Center;
            cell = totalRow.Cells[0];
            p = cell.AddParagraph();
            p.AddFormattedText("Total hours", TextFormat.Bold);
            totalRow.Cells[1].MergeRight = 1;
            p = totalRow.Cells[3].AddParagraph();
            p.AddFormattedText(waitingHours.ToString("N",nfi), TextFormat.Bold);
            totalRow.Cells[4].MergeRight = 1;
            p = totalRow.Cells[6].AddParagraph();
            p.AddFormattedText("$" + totalCost.ToString("N",nfi), TextFormat.Bold);

            //working hours

            //header
            row = table.AddRow();
            row.Borders.Width = 1.25;
            p = row.Cells[0].AddParagraph();
            p.AddFormattedText("WORKING HOURS", TextFormat.Bold);
            row.Cells[0].Format.Alignment = ParagraphAlignment.Center;
            row.Cells[0].MergeRight = 6;

            totalCost = 0;
            float workingHours = 0;
            foreach (DetalleFacturacion c in tipo2)
            {
                row = table.AddRow();
                row.VerticalAlignment = VerticalAlignment.Center;
                row.Cells[0].AddParagraph(c.Fecha.ToString());
                row.Cells[1].AddParagraph(c.HoraIni.ToString());
                row.Cells[2].AddParagraph(c.HoraFin.ToString());
                float hours = c.HorasSubtotales;
                row.Cells[3].AddParagraph(hours.ToString("N",nfi));
                row.Cells[4].AddParagraph(c.Descripcion);
                row.Cells[5].AddParagraph(c.Surveyor);
                row.Cells[6].AddParagraph("$"+c.Costo.ToString("N",nfi));
                totalCost += c.Costo;
                workingHours += hours;
            }
            totalRow = table.AddRow();
            totalRow.VerticalAlignment = VerticalAlignment.Center;
            cell = totalRow.Cells[0];
            p = cell.AddParagraph();
            p.AddFormattedText("Total hours", TextFormat.Bold);
            totalRow.Cells[1].MergeRight = 1;
            p = totalRow.Cells[3].AddParagraph();
            p.AddFormattedText(workingHours.ToString("N",nfi), TextFormat.Bold);
            totalRow.Cells[4].MergeRight = 1;
            p = totalRow.Cells[6].AddParagraph();
            p.AddFormattedText("$"+totalCost.ToString("N",nfi), TextFormat.Bold);

            //espacio para los totales
            row = table.AddRow();
            row.Borders.Visible = false;

            row = table.AddRow();
            row.Borders.Visible = false;
            row.Cells[0].Format.Alignment = ParagraphAlignment.Left;
            row.Cells[3].Format.Alignment = ParagraphAlignment.Right;
            Paragraph bold = row.Cells[0].AddParagraph();
            bold.AddFormattedText("Traveling hours",TextFormat.Bold);
            row.Cells[0].MergeRight = 2;
            bold =row.Cells[3].AddParagraph();
            bold.AddFormattedText(travelingHours.ToString("N",nfi),TextFormat.Bold);

            row = table.AddRow();
            row.Borders.Visible = false;
            row.Cells[0].Format.Alignment = ParagraphAlignment.Left;
            row.Cells[3].Format.Alignment = ParagraphAlignment.Right;
            bold = row.Cells[0].AddParagraph();
            bold.AddFormattedText("Waiting hours", TextFormat.Bold);
            row.Cells[0].MergeRight = 2;
            bold = row.Cells[3].AddParagraph();
            bold.AddFormattedText(waitingHours.ToString("N",nfi), TextFormat.Bold);

            row = table.AddRow();
            row.Borders.Visible = false;
            row.Cells[0].Format.Alignment = ParagraphAlignment.Left;
            row.Cells[3].Format.Alignment = ParagraphAlignment.Right;
            bold = row.Cells[0].AddParagraph();
            bold.AddFormattedText("Working hours", TextFormat.Bold);
            row.Cells[0].MergeRight = 2;
            bold = row.Cells[3].AddParagraph();
            bold.AddFormattedText(workingHours.ToString("N",nfi),TextFormat.Bold);

            row = table.AddRow();
            row.Borders.Visible = false;
            row.Cells[0].Format.Alignment = ParagraphAlignment.Left;
            row.Cells[3].Format.Alignment = ParagraphAlignment.Right;
            bold = row.Cells[0].AddParagraph();
            bold.AddFormattedText("Expenses", TextFormat.Bold);
            row.Cells[0].MergeRight = 2;
            bold = row.Cells[3].AddParagraph();
            bold.AddFormattedText("$" + fact.Expenses.ToString("N",nfi),TextFormat.Bold);
            table.SetEdge(1, table.Rows.Count - 4, 3, 4, Edge.Box | Edge.Interior, BorderStyle.Single, 0.75);
            document.LastSection.Add(table);
            Paragraph footer = new Paragraph();
            footer.Format.Alignment = ParagraphAlignment.Left;
            footer.Format.Font.Color = new Color(35,20,103);
            footer.AddFormattedText("Jr. La Mar 357, Merchant Marine Bldg., Callao, Peru\nPhone: 51-1-5594445 Mobile 51-998347771 (24 hours)\nE-mail equinox@equinox.pe");

            section.Footers.Primary.Add(footer);
        }
        //
        // GET: /pdf/Invoice
        public ActionResult Invoice(int? idCaso, int? idDocumento)
        {
            if (defaultAction != null) return defaultAction;

            // Create a new MigraDoc document
            Caso caso = CasoFunction.abrirCaso(idCaso.Value);
            caso.Costos = FacturacionFunction.listaDetalleFacturacion(idCaso.Value,idDocumento.Value);
            Facturacion fact = FacturacionFunction.abrirFacturacion(idCaso.Value, idDocumento.Value);
            Document document = new Document();
            document.Info.Title = "Invoice";
            document.Info.Author = "Equinox Consulting";
            definirEstilos(document);
            crearPdfInvoice(document,caso,fact);
            const bool unicode = false;
            const PdfFontEmbedding embedding = PdfFontEmbedding.Always;
            PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(unicode, embedding);
            pdfRenderer.Document = document;
            pdfRenderer.RenderDocument();
            MemoryStream stream = new MemoryStream();
            pdfRenderer.PdfDocument.Save(stream,false);
            return File(stream.ToArray(), "application/pdf");
        }

        private void crearPdfInvoice(Document document,Caso caso,Facturacion fact)
        {
            NumberFormatInfo nfi = (NumberFormatInfo)
            CultureInfo.InvariantCulture.NumberFormat.Clone();
            nfi.NumberGroupSeparator = " ";
            document.DefaultPageSetup.TopMargin = "1.5cm";
            document.DefaultPageSetup.LeftMargin="3cm";
            document.DefaultPageSetup.RightMargin = "3cm";
            document.DefaultPageSetup.PageFormat = MigraDoc.DocumentObjectModel.PageFormat.A4;
            string[] referencia = CasoFunction.obtenerReferencia(caso.IdCaso).Split("/".ToCharArray());
            string strFormat = "F";
            Section section = document.AddSection();
            //Imagen y nombre de la empresa
            Paragraph nombre = document.LastSection.AddParagraph();
            nombre.Style = "Heading1";
            nombre.Format.Alignment = ParagraphAlignment.Center;
            nombre.Format.Font.Color = Colors.DodgerBlue;
            Image img = nombre.AddImage(Server.MapPath("~/Content/site/images/logo-small-text.jpg"));
            //nombre.AddText("EQUINOX CONSULTING S.A.C.");
            document.LastSection.AddParagraph("\n\n");
            //Titulo del documento
            Paragraph titulo = document.LastSection.AddParagraph();
            titulo.Format.Alignment = ParagraphAlignment.Center;
            titulo.Style = "Heading2";
            titulo.AddText("PRO-FORMA INVOICE\t" + referencia[0] + " / " + referencia[1] + " / " + referencia[2]);

            //To
            Paragraph to = document.LastSection.AddParagraph("\n\nTo: Messrs.", "Heading3");
            to.Format.Alignment = ParagraphAlignment.Left;
            to.AddText("\n" + (fact.Facturara == null || fact.Facturara == "" ? caso.Cliente.RazonSocial : fact.Facturara));

            document.LastSection.AddParagraph("\n");

            Table tabla = new Table();
            //tabla.Borders.Width = 0.75;
            tabla.AddColumn(Unit.FromCentimeter(2));
            tabla.AddColumn(Unit.FromCentimeter(13));
            document.LastSection.Add(tabla);
            //Ref
            Row fila = tabla.AddRow();
            Paragraph Ref = fila.Cells[0].AddParagraph();
            Ref.AddFormattedText("Ref:", "Heading3");
            Ref.Format.Alignment = ParagraphAlignment.Left;

            Paragraph RefData = fila.Cells[1].AddParagraph();
            RefData.Format.Alignment = ParagraphAlignment.Justify;
            RefData.AddText(caso.Content == null ? "":caso.Content);
            tabla.AddRow();

            //Rate
            fila = tabla.AddRow();
            Paragraph rate = fila.Cells[0].AddParagraph();
            rate.AddFormattedText("Rate:", "Heading3");

            Paragraph rateData = fila.Cells[1].AddParagraph();
            rateData.Format.Alignment = ParagraphAlignment.Justify;
            rateData.AddText("US $ "+fact.Rate+" x working hour / half for traveling and waiting hour / expenses at cost / Saturday, Sunday and Holiday 30 % on a normal day");

            //Tabla de detalle
            Paragraph desc = document.LastSection.AddParagraph();
            desc.AddText("\nFor services rendered in connection with survey and report.\n\n\n");

            Table table = new Table();
            //table.Borders.Width = 0.75;
            table.AddColumn(Unit.FromCentimeter(2));
            Column column = table.AddColumn(Unit.FromCentimeter(6));
            column.Format.Alignment = ParagraphAlignment.Left;

            column = table.AddColumn(Unit.FromCentimeter(3.5));
            column.Format.Alignment = ParagraphAlignment.Right;
            column = table.AddColumn(Unit.FromCentimeter(2.5));
            float total = 0;
            foreach (DetalleFacturacion c in caso.Costos)
            {
                total += c.Costo;
            }
            Row row = table.AddRow();
            Cell cell = row.Cells[1];
            //cell.Format.LeftIndent = "3cm";
            cell.AddParagraph("Total Fees");
            cell = row.Cells[2];
            //cell.Format.RightIndent = "3cm";
            cell.AddParagraph("$ " + total.ToString(strFormat));

            row = table.AddRow();
            cell = row.Cells[1];
            //cell.Format.LeftIndent = "3cm";
            cell.AddParagraph("Expenses");
            cell = row.Cells[2];
            //cell.Format.RightIndent = "3cm";
            cell.AddParagraph("$ " + fact.Expenses.ToString(strFormat));

            row = table.AddRow();
            cell = row.Cells[1];
            //cell.Format.LeftIndent = "3cm";
            cell.AddParagraph("IVA (18%)");
            cell = row.Cells[2];
            //cell.Format.RightIndent = "3cm";
            double iva = (fact.Expenses + total) * 0.18;
            cell.AddParagraph("$ " + iva.ToString(strFormat));

            Row totalRow = table.AddRow();
            Cell totalCell = totalRow.Cells[1];
            //totalCell.Format.LeftIndent = "3cm";
            Paragraph p = totalCell.AddParagraph();
            p.AddFormattedText("Total Fees",TextFormat.Bold);

            totalCell = totalRow.Cells[2];
            //totalCell.Format.RightIndent = "3cm";
            p = totalCell.AddParagraph();
            p.AddFormattedText("$ " + (total + fact.Expenses + iva).ToString(strFormat), TextFormat.Bold);

            table.SetEdge(1, table.Rows.Count - 1, 2, 1, Edge.Top, BorderStyle.Single, 0.75);
            document.LastSection.Add(table);

            //Titulo instrucciones
            Paragraph instructions = document.LastSection.AddParagraph("\n\n", "Heading3");
            instructions.Format.Alignment = ParagraphAlignment.Center;
            instructions.AddFormattedText("SETTLEMENTS INSTRUCTIONS",TextFormat.Underline);

            //parrafo detalle
            Paragraph inData = document.LastSection.AddParagraph();
            inData.Format.Alignment = ParagraphAlignment.Left;
            inData.AddText("PLEASE PAY BY WAY OF BANKER TRANSFER IN ACCORDANCE WITH THE FOLLOWING DETAILS: \n");

            //datos detalle
            Paragraph det1 = document.LastSection.AddParagraph("","Heading3");
            det1.Format.Alignment = ParagraphAlignment.Left;
            //det1.Format.LeftIndent = "2.5cm";
            det1.AddText("BANK\t\t\t\tBANCO DE CREDITO DEL PERU\n");
            
            Paragraph det2 = document.LastSection.AddParagraph("","Heading3");
            det2.Format.Alignment = ParagraphAlignment.Left;
            //det2.Format.LeftIndent = "2.5cm";
            det2.AddText("ADDRESS\t\t\tJIRON LAMPA 499-LIMA 01-PERU\n");

            Paragraph det3 = document.LastSection.AddParagraph("","Heading3");
            det3.Format.Alignment = ParagraphAlignment.Left;
            //det3.Format.LeftIndent = "2.5cm";
            det3.AddText("SWIFT CODE\t\tBCPLPEPL\n");

            Paragraph det4 = document.LastSection.AddParagraph("","Heading3");
            det4.Format.Alignment = ParagraphAlignment.Left;
            //det4.Format.LeftIndent = "2.5cm";
            det4.Format.RightIndent = "1.2cm";
            det4.AddText("ACCOUNT NUMBER\tU.S. DOLARES Nº 193-1541105-1-26\n");
            det4.Format.Borders.Width = 1.5;
            det4.Format.Borders.Color = Colors.Black;
            det4.Format.Borders.Distance = 3;

            Paragraph det5 = document.LastSection.AddParagraph("","Heading3");
            det5.Format.Alignment = ParagraphAlignment.Left;
            //det5.Format.LeftIndent = "2.5cm";
            det5.AddText("BENEFICIARY\t\tEQUINOX CONSULTING SAC\n");

            Paragraph footer = new Paragraph();
            footer.Format.Font.Color = new Color(35, 20, 103);
            footer.Format.Font.Size = 10;
            footer.Format.Alignment = ParagraphAlignment.Left;
            footer.AddText("Jr. La Mar 357, Merchant Marine Bldg., Callao, Peru\nPhone: 51-1-5594445 Mobile 51-998347771 (24 hours)\nE-mail equinox@equinox.pe");

            section.Footers.Primary.Add(footer);
        }

        private void definirEstilos(Document document)
        {
            //obtener el estilo "Normal" y editar desde ahi
            Style style = document.Styles["Normal"];
            //poner esta font para todo el dcumento
            style.Font.Name = "Times New Roman";
            style.Font.Size = 12;

            style = document.Styles["Heading1"];
            style.Font.Size = 16;
            style.Font.Bold = true;
            style.ParagraphFormat.PageBreakBefore = true;
            style.ParagraphFormat.SpaceBefore = 6;
            style.ParagraphFormat.SpaceAfter = 6;

            style = document.Styles["Heading2"];
            style.Font.Size = 14;
            style.Font.Bold = true;
            style.ParagraphFormat.PageBreakBefore = false;
            style.ParagraphFormat.SpaceBefore = 6;
            style.ParagraphFormat.SpaceAfter = 6;

            style = document.Styles["Heading3"];
            style.Font.Size = 12;
            style.Font.Bold = true;
            style.ParagraphFormat.PageBreakBefore = false;
            style.ParagraphFormat.SpaceBefore = 6;
            style.ParagraphFormat.SpaceAfter = 6;

            style = document.Styles["Heading4"];
            style.Font.Size = 10;
            style.Font.Bold = true;
            style.ParagraphFormat.PageBreakBefore = false;
            style.ParagraphFormat.SpaceBefore = 6;
            style.ParagraphFormat.SpaceAfter = 6;


        }


        //
        // GET: /pdf/

        public ActionResult Index()
        {
            return View();
        }

    }
}
